<?php
namespace app\index\controller;

use app\common\model\ZhcheckinMess as ZhcheckinMessModel;
use app\common\model\ZhCheckinLog as ZhCheckinLogModel;
use app\common\model\Company as CompanyModel;

/**
 * 入住控制器
 * Class Zhcheckinmess
 * @package app\index\controller
 */
class Zhcheckinmess extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhcheckinMessModel;
        $cmodel = new ZhCheckinLogModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        try{
            if(!empty($param['con'])){
                $map['tel|name|idcard|company_name'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['is_ascetic'])){
                $map['is_ascetic'] = $param['is_ascetic'];
            }
            if(!empty($param['sex'])){
                $map['sex'] = $param['sex'];
            }
            if(!empty($param['city'])){
                $map['company_city'] = $param['city'];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            if(!empty($param['mid'])){
                $map['mid'] = $param['mid'];
            }
            $map['comid'] = $user['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size);
            foreach($lists as &$v){
                $v['ruzhu_time'] = date('Y-m-d H:i:s',strtotime($v['create_time']));
                $log2 = $cmodel->getFindOne(['type'=>2,'cid'=>$v['id']],'time');
                if(empty($log2)){
                    $v['yuji_leave_time'] = date('Y-m-d H:i:s',strtotime($v['create_time']) + $v['reside_day'] * 24*3600);
                }else{
                    $v['yuji_leave_time'] = date('Y-m-d H:i:s',$log2['time']);
                }

                $log = $cmodel->getFindOne(['type'=>1,'cid'=>$v['id']],'time');
                $log1 = $cmodel->getFindOne(['cid'=>$v['id']],'type');
                if(!empty($log)){
                    $v['leave_time'] = date('Y-m-d H:i:s',$log['time']);
                    $ruzhu = strtotime(date('Y-m-d',strtotime($v['create_time'])));
                    $leave = strtotime(date('Y-m-d',$log['time']));
                    $v['reside_day1'] = ($leave - $ruzhu)/86400;
                }
                if(!empty($log1)){
                    $v['other_type'] = $log1['type'];
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [edit  编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new ZhcheckinMessModel;
        $user = $this->user;
        try{
            $data = $model->findData($param['id'],'id','comid');
            if($data['comid'] != $user['comid']){
                return admin_json(-1,'编辑失败');
            }
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '编辑成功');
    }
    /**
     * [getone  获取单挑入住记录]
     * @return json
     * @author [C。]
     */
    public function getone()
    {
        $param = input('param.');
        $model = new ZhcheckinMessModel;
        $cmodel = new CompanyModel;
        $user = $this->user;
        try{
            $company = $cmodel->findData($param['comid'],'id','compic,class_phone,addr');
            $http = substr($company['compic'],0,4);
            if($http != 'http'){
                $company['compic'] = config('domain_name').$company['compic'];
            }
            if(!empty($param['id'])){
                $data = $model->getFindOne(['id'=>$param['id'],'mid'=>$user['id']]);
                $data['compic'] = $company['compic'];
                $data['class_phone'] = $company['class_phone'];
                $data['addr'] = $company['addr'];
                $data['ruzhu_time'] = date('Y-m-d',strtotime($data['create_time']));
                $data['leave_time'] = date('Y-m-d',strtotime($data['ruzhu_time'])+$data['reside_day']*24*3600);
            }else{
                $data = $company;
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}